//
//  ITTouchIdViewController.m
//  InduceTool
//
//  Created by 魏延龙 on 2020/9/21.
//  Copyright © 2020 魏延龙. All rights reserved.
//

#import "ITTouchIdViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>

@interface ITTouchIdViewController ()

//本地认证上下文联系对象
@property (nonatomic, strong) LAContext *context;

@end

@implementation ITTouchIdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    [self setupViews];
}

- (void)setupViews {
    UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [startBtn setTitle:@"登录" forState:UIControlStateNormal];
    startBtn.frame = CGRectMake(50, 200, 100, 44);
    [self.view addSubview:startBtn];
    [startBtn addTarget:self action:@selector(startAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)startAction {
    //本地认证上下文联系对象，每次使用指纹识别验证功能都要重新初始化，否则会一直显示验证成功。
    self.context = [[LAContext alloc] init];
    NSError *error = nil;
    //验证是否具有指纹认证功能
    BOOL canEvaluatePolicy = [_context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error];
    if (error) {
        NSLog(@"%@", error.localizedDescription);
    }

    if (canEvaluatePolicy) {
        NSLog(@"有指纹认证功能");
        // 指纹认证错误后的第二个按钮文字（不写默认为“输入密码”）
        _context.localizedFallbackTitle = @"芝麻开门";
        // 调用指纹验证
        [self beginTouchId1];
    } else {
        NSLog(@"无指纹认证功能");
    }
}

- (void)beginTouchId1 {
    [_context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:@"验证指纹以确认您的身份" reply:^(BOOL success, NSError *error) {
        // 切换到主线程
        dispatch_async(dispatch_get_main_queue(), ^{
                           if (success) {
                               NSLog(@"指纹验证成功");
                           } else {
                               NSLog(@"指纹认证失败，%@", error.description);
                               NSLog(@"%ld", (long)error.code);
                               // 错误码 error.code
                               switch (error.code) {
                                   case LAErrorUserCancel: { NSLog(@"用户取消验证Touch ID");// -2 在TouchID对话框中点击了取消按钮或者按了home键
                                   }
                                   break;

                                   case LAErrorUserFallback: {
                                       NSLog(@"用户选择输入密码"); // -3 在TouchID对话框中点击了输入密码按钮
                                   }
                                   break;

                                   case LAErrorSystemCancel: { NSLog(@"取消授权，如其他应用切入，用户自主"); // -4 TouchID对话框被系统取消，例如按下电源键
                                   }
                                   break;

                                   case LAErrorPasscodeNotSet: {
                                       NSLog(@"设备系统未设置密码"); // -5
                                   }
                                   break;

                                   case LAErrorTouchIDNotAvailable: {
                                       NSLog(@"设备未设置Touch ID"); // -6
                                   }
                                   break;

                                   case LAErrorTouchIDNotEnrolled:  {
                                       NSLog(@"用户未录入指纹"); // -7
                                   }
                                   break;

                                   case LAErrorAppCancel: {
                                       NSLog(@"用户不能控制情况下APP被挂起"); // -9
                                   }
                                   break;

                                   case LAErrorInvalidContext: {
                                       NSLog(@"LAContext传递给这个调用之前已经失效"); // -10
                                   }
                                   break;

                                   default: {
                                       NSLog(@"其他情况");
                                   }
                                   break;
                               }
                           }
                       });
    }];
}

@end
